package com.silverhaze.diamond.client.ui.dialogs;

import com.silverhaze.diamond.common.entity.Detail;
import com.silverhaze.diamond.common.entity.Service;
import com.silverhaze.diamond.client.data.dao.DAOLocator;
import com.silverhaze.diamond.client.resources.ServiceResourceContainer;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.UIStringConstants;
import com.silverhaze.diamond.client.utils.db.ServiceDataSource;
import com.silverhaze.commons.ui.layout.SpringUtilities;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Author: Ivan Babanin
 * Time: 14:01:11
 * Date: 21.05.2008
 * <p/>
 * This part of code is strictly confidential.
 */
public class DetailDialog extends AbstractDialog {
    private JTextField nameField;
    private DialogType dialogType;
    private JTextArea descriptionArea;

    private Detail detail;
    private boolean edit;
    private boolean view;

    private DialogConnector dialogConnector;

    public DetailDialog(Frame owner, Detail detail, DialogType dialogType, DialogConnector dialogConnector) {
        super(owner);
        setIconImage(ServiceResourceContainer.SERVICE.getImage());

        this.detail = detail;
        this.dialogType = dialogType;
        edit = (detail != null);

        if (dialogType == DialogType.EDIT) {
            setTitle(UIStringConstants.EDIT_DETAIL);
        } else if (dialogType == DialogType.CREATE) {
            setTitle(UIStringConstants.ADD_DETAIL);
        } else {
            setTitle("Просмотр детали");
        }

        this.dialogConnector = dialogConnector;

        initView();
    }

    private void initView() {
        JPanel panel = new JPanel(new SpringLayout());

        nameField = createTextField(edit ? detail.getName() : "", 15);

        descriptionArea = createTextArea(edit ? detail.getDescription() : "", 3, 15);
        descriptionArea.setBorder(new LineBorder(Color.BLACK, 1));
        descriptionArea.setLineWrap(true);

        panel.add(new JLabel(UIStringConstants.DETAIL + ": "));
        panel.add(nameField);

        panel.add(new JLabel(UIStringConstants.DESCRIPTION + ": "));
        panel.add(new JScrollPane(descriptionArea));

        Box hBox = Box.createHorizontalBox();

        final java.util.List<Service> byDetail = DAOLocator.getInstance().getServices(detail);
        if (detail != null && byDetail.size() != 0) {
            if (dialogType == DialogType.VIEW) {
                hBox.add(new JButton(new AbstractAction("Сервисы использующие деталь") {
                    public void actionPerformed(ActionEvent e) {
                        SpecificServicesDialog.view(byDetail, detail);
                    }
                }));
            }
        }

        if (dialogType != DialogType.VIEW) {
            hBox.add(new JButton(new AbstractAction(edit ? UIStringConstants.SAVE : UIStringConstants.ADD) {
                public void actionPerformed(ActionEvent e) {
                    okPressed();
                }
            }));
        }

        hBox.add(Box.createHorizontalStrut(2));
        hBox.add(new JButton(new AbstractAction(dialogType == DialogType.VIEW ?
                "Закрыть" : UIStringConstants.CANCEL) {
            public void actionPerformed(ActionEvent e) {
                cancelPressed();
            }
        }));

        panel.add(new JLabel());
        panel.add(hBox);

        SpringUtilities.makeCompactGrid(panel, 3, 2, 10, 10, 10, 10);
        add(panel, BorderLayout.CENTER);

        pack();
        setLocationRelativeTo(UIHelper.getMainFrame());
    }

    protected void okPressed() {
        if (checkFields()) {
            if (edit) {
                detail.setName(nameField.getText().trim());
                detail.setDescription(descriptionArea.getText().trim());

                ServiceDataSource.getInstance().update(detail);
            } else {
                Detail d = new Detail();

                d.setName(nameField.getText().trim());
                d.setDescription(descriptionArea.getText().trim());
                d.setId(System.currentTimeMillis());

                DAOLocator.getInstance().add(d);

                if (dialogConnector != null) {
                    dialogConnector.objectSelected(d);
                }
            }

            dispose();
        } else {
            Dialogs.showErrorDialog(null, UIStringConstants.PLEASE_FILL_FIELDS);
        }
    }

    private boolean checkFields() {
        return !(nameField.getText().trim().equals("") || descriptionArea.getText().trim().equals(""));
    }

    protected void cancelPressed() {
        dispose();
    }

    public static void createDetail(Frame parent, DialogConnector dialogConnector) {
        DetailDialog detailDialog = new DetailDialog(parent, null, DialogType.CREATE, dialogConnector);
        detailDialog.setVisible(true);
    }

    public static void editDetail(Frame parent, Detail detail) {
        DetailDialog detailDialog = new DetailDialog(parent, detail, DialogType.EDIT, null);
        detailDialog.setVisible(true);
    }

    public static void viewDetail(Detail detail) {
        DetailDialog detailDialog = new DetailDialog(null, detail, DialogType.VIEW, null);
        detailDialog.enableInput(false);
        detailDialog.setVisible(true);
    }

    public static void createClone(Frame parent, Detail selectedDetail) {
        DetailDialog detailDialog = new DetailDialog(parent, null, DialogType.CREATE, null);
        detailDialog.initializeFieldsWith(selectedDetail);
        detailDialog.setVisible(true);
    }

    private void initializeFieldsWith(Detail selectedDetail) {
        nameField.setText(selectedDetail.getName());
        descriptionArea.setText(selectedDetail.getDescription());
    }
}

